package ch.mueller.android.todo.view.appwidget;

import java.util.zip.Inflater;

import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.view.LayoutInflater;
import android.widget.RemoteViews;
import android.widget.TextView;
import android.widget.Toast;
import ch.mueller.android.todo.R;
import ch.mueller.android.todo.ToDo;
import ch.mueller.android.todo.controller.ToDo_SQLOpenHelper;
import ch.mueller.android.todo.model.TodoItems;

public class MyTodoAppwidgetprovider extends AppWidgetProvider{

    @Override
    public void onReceive(Context context, Intent intent) {

        if (intent.getAction().equals("UPADTE_WIDGET")) {

            try {

                AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);

                ComponentName componentName = new ComponentName(context.getPackageName(),
                        "ch.mueller.android.todo.view.appwidget.MyTodoAppwidgetprovider");
                int[] appWidgetIds = appWidgetManager.getAppWidgetIds(componentName);

                this.onUpdate(context, appWidgetManager, appWidgetIds);

            } catch (Exception e) {

                Toast.makeText(context, "error: " + e.getMessage(), Toast.LENGTH_SHORT).show();
            }

        }

        super.onReceive(context, intent);
    }

    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {

        ToDo_SQLOpenHelper sqlOpenHelper = new ToDo_SQLOpenHelper(context);
        SQLiteDatabase database = sqlOpenHelper.getReadableDatabase();

        Cursor cursor = database.query(TodoItems.TABLE_NAME, null, null, null, null, null, TodoItems.TC_PRIORITY);

        String[] values = { "", "", "" };
        int n = 0;
        while (cursor.moveToNext() && n < 3) {

            values[n++] = cursor.getString(cursor.getColumnIndex(TodoItems.TC_PRIORITY)) + " "
                    + cursor.getString(cursor.getColumnIndex(TodoItems.TC_TITLE));
        }

        for (int i = 0; i < appWidgetIds.length; i++) {

            int appWidgetId = appWidgetIds[i];

            RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.mytodo_appwidget);
               
            remoteViews.setCharSequence(R.id.appwidget_firstelement, "setText", values[0]);
            remoteViews.setCharSequence(R.id.appwidget_secondelement, "setText", values[1]);
            remoteViews.setCharSequence(R.id.appwidget_thirdelement, "setText", values[2]);

            PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, new Intent(context,
                    ToDo.class), 0);

            remoteViews.setOnClickPendingIntent(R.id.appwidget_parentlayout, pendingIntent);

            appWidgetManager.updateAppWidget(appWidgetId, remoteViews);

            super.onUpdate(context, appWidgetManager, appWidgetIds);
        }
    }

}
